﻿
using System;
using Guia.Elements;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WpfTestApplication;
using Guia.Core;
using System.Windows.Automation;

namespace GuiaTest
{
    [TestClass]
    public class ContextMenuTest : AbstractWindowTest
    {
        public ContextMenuTest()
        {
        }

        protected override Type TestWindowType()
        {
            return typeof(ContextMenuTestWindow);
        }

        protected override string TestWindowName()
        {
            return "ContextMenuTestWindow";
        }

        [TestMethod]
        [ExpectedException(typeof(ElementNotFoundException))]
        public void TestShowContextMenuNotFound()
        {
            ContextMenu cm = window.Get<ContextMenu>("cm");
            Assert.IsNull(cm);
        }

        [TestMethod]
        public void TestShowContextMenuAfterRightClick()
        {
            window.Get<ListBox>("listBox").ClickRight();
            ContextMenu cm = window.Get<ContextMenu>("cm");
            Assert.IsNotNull(cm);
        }

        [TestMethod]
        public void TestContextMenuClickAction()
        {
            window.Get<ListBox>("listBox").ClickRight();
            window.Get<ContextMenu>("cm")["miCopy"].Invoke();
            Assert.AreEqual("Copy", window.Get<Label>("labelContextMenuClicked").TextValue);
        }

        [TestMethod]
        public void TestContextMenuClickActionSubmenu()
        {
            window.Get<ListBox>("listBox").ClickRight();
            window.Get<ContextMenu>("cm")["miFurther"]["miFurther1"].Invoke();
            Assert.AreEqual("Action1", window.Get<Label>("labelContextMenuClicked").TextValue);
        }

        [TestMethod]
        public void TestContextMenuClickActionSubmenuParentWindow()
        {
            ListBox listBox = window.Get<ListBox>("listBox");
            listBox.ClickRight();
            Window parentWindow = new Window(listBox.ParentWindow.AutomationElement);
            parentWindow.Get<ContextMenu>("cm")["miFurther"]["miFurther1"].Invoke();
            Assert.AreEqual("Action1", window.Get<Label>("labelContextMenuClicked").TextValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ElementNotEnabledException))]
        public void TestContextMenuClickMenuDisabled()
        {
            window.Get<ListBox>("listBox").ClickRight();
            window.Get<ContextMenu>("cm")["miFurther"]["miFurther2"].Invoke();
        }

        [TestMethod]
        public void TestContextMenuClickMenuCheckUnCheck()
        {
            window.Get<ListBox>("listBox").ClickRight();
            window.Get<ContextMenu>("cm")["miOptions"]["miOption1"].Check();
            Assert.IsTrue(window.Get<ContextMenu>("cm")["miOptions"]["miOption1"].IsChecked);
            window.Get<ListBox>("listBox").ClickRight();
            window.Get<ContextMenu>("cm")["miOptions"]["miOption1"].UnCheck();
            Assert.IsFalse(window.Get<ContextMenu>("cm")["miOptions"]["miOption1"].IsChecked);
        }
    }
}
